<template>
	<view class="patientcard">
		<view class="cardcontent">
			<u-row class="content_text">
				<u-col span="3" >
					<view class="second-font">
						预约时间
					</view>
				</u-col>
				<u-col span="5" >
					<view class="first-font">
						{{patient.consultCreateTime}}
					</view>
				</u-col>
				<u-col offset="2" span="2" >
					<view class="impotant-font">
						{{patient.consultStatus}}
					</view>
				</u-col>
			</u-row>
			<view class="separate" ></view>
			<view @click="toPath()">
				<u-row class="content_text">
					<u-col span="2">
						<u-avatar></u-avatar>
					</u-col>
					<u-col span="9" >
						<view class="content_text2">
							<u-row>
								<u-col span="6" class="second-font">
									<view>{{patient.personName}}</view>
								</u-col>
								<u-col offset="1" span="1" >
									<view class="third-font">{{patient.personGenderName}}</view>
								</u-col>
								<u-col offset="1" span="3">
									<view class="third-font">{{patient.personAge}}岁</view>
								</u-col>
							</u-row>
							<u-row>
								<u-col span="4" class="second-font">
									<view>药品需求</view>
								</u-col>
								<u-col span="8" class="first-font">
									<view class="medicine_info">{{patient.drugIds}}</view>
								</u-col>
							</u-row>
						</view>
					</u-col>
					<u-col  span="1" >
						<view class="content_text2">
							<u-icon name="arrow-right" size="44"></u-icon>
						</view>
					</u-col>
				</u-row>	
			</view>			
			<view class="separate"></view>
			<u-row class="content_text">
				<u-col offset="9" span="3" >
					<view v-if="patient.consultStatus=='待接诊' || patient.consultStatus=='进行中'">
						<u-button type="primary" plain=true size="mini" @click="chooseconfirmdispensing(index)">完成确诊</u-button>
					</view>
					<view v-if="patient.consultStatus=='已完成'">
						<u-button type="primary" plain=true size="mini" @click="toPath2()">查看处方</u-button>
					</view>
				</u-col>
			</u-row>
		</view>
	</view>
</template>

<script>
export default {
	props:["patient","index"],
	data() {
		return {
			patientlist:[]
			// patientlist:[
			// 	{
			// 		patientnum:0,
			// 		statement:'待完成',
			// 		datetime:'2021-7-14 13:41:00',
			// 		name:'王小明',
			// 		gender:'男',
			// 		age:'12',
			// 		demand:'药品药品药品药品药品药品药品药品药品药品药品药品药品药品'
			// 	}
			// ]
		};
	},
	methods:{
		toPath(){
			uni.navigateTo({
				url:"doctor_dispensing_detail?id="+this.patient.consultId
			})
		},
		toPath2(){
			uni.navigateTo({
				url:"../prescription/doctor_prescriptionlist_detail?id="+this.index
			})
		},
		chooseconfirmdispensing(index){
			var consult=this.patient.consultId;
			uni.showModal({
				title: '提示',
				content: '是否提交处方？',
				success: function (res) {
				    if (res.confirm) {
						getApp().globalData.worktable.forEach(function(temp){
							var drugsinfo=[];
							var preid=0
							uni.request({
							    url: getApp().globalData.hostip+'Demo/preInfo/add', 
								method: 'POST',
							    data:{
									consultId:consult,
									prescriptionType:'西药',
								},
								header: {
									'Content-Type': 'application/x-www-form-urlencoded',
								},
								fail:(fa) =>{
									console.log(fa)
								},
							    success: (res) => {
									preid=res.data.preId
									console.log(res.data)
									temp.drugs.forEach(function(temp2){
										drugsinfo.push({
											'drugId':temp2.drugId,
											'usageCode':temp2.usageCode,
											'frequencyCode':temp2.frequencyCode,
											'takeDays':temp2.takeDays,
											'quantity':temp2.quantity,
											'remark':temp2.remark,
											'groupNumber':1,
											'sortNumber':1
										})
										console.log(temp.drugs)
										uni.request({
										    url: getApp().globalData.hostip+'Demo/preDrug/add', 
											method: 'POST',
										    data:{
												usageCode:temp2.usageCode.toString(),
												drugId:temp2.drugId.toString(),
												prescriptionId:preid.toString(),
												frequencyCode:temp2.frequencyCode.toString(),
												takeDays:temp2.takeDays.toString(),
												quantity:temp2.quantity.toString(),
												groupNumber:"1",
												sortNumber:"1",
												remark:temp2.remark
											},
											header: {
												'Content-Type': 'application/json',
											},
											fail:(fa) =>{
												console.log(fa)
											},
										    success: (res) => {
												console.log(res.data)
										    }
										});
									});
									uni.request({
										url: getApp().globalData.hostip+'Demo/preInfo/updateSubmit?id='+preid,
										data: {
										},
										fail:(fa) =>{
											console.log(fa)
										},
										success: (res) => {
											console.log(res.data)
										},
									})
							    }
							});
							getApp().globalData.patientlist[index].prescriptions.push({
								'prescriptionType':'',
								'prescriptionCreateTime':'',
								'drugs':drugsinfo,
							})
						})
						getApp().globalData.patientlist[index].dispensings=getApp().globalData.worktable;
						// console.log(getApp().globalData.patientlist[index].prescriptions);
						// console.log(getApp().globalData.patientlist[index]);
						getApp().globalData.patientlist[index].consultStatus='已完成'
						getApp().globalData.worktable=[]
						setTimeout(function () {
						    uni.request({
						    	url: getApp().globalData.hostip+'Demo/consult/updateFinish?id='+getApp().globalData.patientlist[index].consultId,
						    	data: {
						    	},
						    	fail:(fa) =>{
						    		console.log(fa)
						    	},
						    	success: (res) => {
									console.log(getApp().globalData.patientlist[index].consultId)
						    		console.log(res.data)
						    	},
						    })
						}, 2000);
				    }
				}
			});
			this.$goEasy.publish({
				channel: this.patient.accountId, //替换为您自己的channel
				message: "您的接诊已完成" //替换为您想要发送的消息内容
			});
		}
	},
	created(){
		this.patientlist=getApp().globalData.patientlist
	}
};
</script>

<style scoped lang="scss">
	.patientcard{
		width: 100%;
		color: #d4d4d4;
		border: 1px solid;
		background-color: #FFFFFF;
		margin-top: 20rpx;
	}
	.u-row{
		margin: 20rpx 20rpx;
	}
	.medicine_info{
		width: 300rpx;
		display: -webkit-box; /** 对象作为伸缩盒子模型显示 **/
		overflow: hidden;
		word-break: break-all;  /* break-all(允许在单词内换行。) https://www.w3school.com.cn/cssref/pr_word-break.asp*/
		text-overflow: ellipsis;  /* 超出部分省略号 */
		-webkit-box-orient: vertical; /** 设置或检索伸缩盒对象的子元素的排列方式 **/
		-webkit-line-clamp: 1; /** 显示的行数 **/
	}
	.content_text{
		padding: 10px;
	}
	.content_text2{
		margin-top: 12rpx;
	}
</style>